#!/usr/bin/env python3
# _*_ coding:utf-8 _*_


class Pagination(object):
    def __init__(self, current_page, all_count, base_url, query_params, per_page=10, pager_page_count=7):
        self.base_url = base_url

        self.query_params = query_params
        self.all_count = all_count
        self.per_page = per_page
        self.pager_page_count = pager_page_count
        pager_count, b = divmod(all_count, per_page)
        if b != 0:
            pager_count += 1
        self.pager_count = pager_count

        half_pager_page_count = int(pager_page_count / 2)
        self.half_pager_page_count = half_pager_page_count
        try:
            self.current_page = int(current_page)
            if self.current_page <= 0:
                raise Exception()
            elif self.current_page > self.pager_count:
                self.current_page = self.pager_count
        except Exception as e:
            self.current_page = 1

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page

    @property
    def end(self):
        return self.current_page * self.per_page

    def page_html(self):
        if self.all_count >= 1:
            if self.current_page <= self.half_pager_page_count + 1:
                pager_start = 1
                if self.pager_count < self.pager_page_count:
                    pager_end = self.pager_count
                else:
                    pager_end = self.pager_page_count

            else:
                if (self.current_page + self.half_pager_page_count) > self.pager_count:
                    pager_end = self.pager_count
                    if self.pager_count > self.pager_page_count:
                        pager_start = self.pager_count - self.pager_page_count + 1
                    else:
                        pager_start = 1
                else:
                    pager_start = self.current_page - self.half_pager_page_count
                    pager_end = self.current_page + self.half_pager_page_count
            page_list = []
            if self.current_page <= 1:
                first = '<li class="footable-page-arrow disabled"><a  href="?page=1">首页</a></li>'
                prev = '<li class="footable-page-arrow disabled"><a href="#">上一页</a></li>'
            else:
                self.query_params['page'] = 1
                first = '<li class="footable-page-arrow "><a  href="%s?%s">首页</a></li>' % (
                    self.base_url, self.query_params.urlencode())

                self.query_params['page'] = self.current_page - 1
                prev = '<li class="footable-page-arrow "><a href="%s?%s">上一页</a></li>' % (
                    self.base_url, self.query_params.urlencode())
            page_list.append(first)
            page_list.append(prev)

            for i in range(pager_start, pager_end + 1):
                self.query_params['page'] = i
                if self.current_page == i:
                    tpl = '<li class="footable-page active"><a href="%s?%s">%s</a></li>' % (
                        self.base_url,
                        self.query_params.urlencode(),
                        i
                    )
                else:
                    tpl = '<li class="footable-page"><a href="%s?%s">%s</a></li>' % (
                        self.base_url,
                        self.query_params.urlencode(),
                        i
                    )
                page_list.append(tpl)
            if self.current_page >= self.pager_count:
                nxt = '<li class="footable-page-arrow disabled"><a href="#">下一页</a></li>'
                end = ' <li class="footable-page-arrow disabled"><a href="#">尾页</a></li>'
            else:
                self.query_params['page'] = self.pager_count
                end = ' <li class="footable-page-arrow "><a href="%s?%s">尾页</a></li>' % (
                    self.base_url,
                    self.query_params.urlencode()
                )
                self.query_params['page'] = self.current_page + 1
                nxt = '<li class="footable-page-arrow "><a href="%s?%s">下一页</a></li>' % (
                    self.base_url,
                    self.query_params.urlencode()
                )

            page_list.append(nxt)
            page_list.append(end)
            page_str = "".join(page_list)
            return page_str
        else:
            return ''
